import { isBrowser } from './util'
import nativeApp from './nativeApp'
import { deviceType } from './deviceType'

export function goConsultation(item, hospital, product_id) {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    const app = new nativeApp()
    if (item.product_type === 'sw') {
      if (app.isInApp()) {
        window.rcatAppInject.launchNative('rcat://ImLanch?id=100451&name=官方医美管家&userPhoto=https://img.rongcat.com/images/yimeiguanjia.jpeg')
      } else {
        targetAppOrWx(product_id)
      }
    } else {
      if (app.isInApp()) {
        window.rcatAppInject.launchNative(`rcat://ImLanch?im_id=${hospital.im_id}&user_id=${hospital.user_id}&user_name=${hospital.user_name}&user_photo=${hospital.user_photo}&hook_id=${hospital.user_id}&id=${hospital.im_id}&name=${hospital.user_name}&hook_user_id=${hospital.user_id}`)
      } else {
        targetAppOrWx(product_id)
      }
    }
  }
}

export function goHospitaConsultation(hospital) {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    const app = new nativeApp()
    if (app.isInApp()) {
      window.rcatAppInject.launchNative(`rcat://ImLanch?im_id=${hospital.im_id}&user_id=${hospital.user_id}&user_name=${hospital.user_name}&user_photo=${hospital.user_photo}&hook_id=${hospital.user_id}&id=${hospital.im_id}&name=${hospital.user_name}&hook_user_id=${hospital.user_id}`)
    } else {
      window.location.href = `https://applink.rongcat.com/hospital/${hospital.user_id}`
    }
  }
}

export function goFlashSale() {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    const app = new nativeApp()
    if (app.isInApp()) {
      window.rcatAppInject.launchNative(`rcat://FlashSaleDetail`)
    } else {
      window.location.href = `https://applink.rongcat.com/`
    }
  }
}

export function goHospital(id) {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    const app = new nativeApp()
    if (app.isInApp()) {
      window.rcatAppInject.launchNative(`rcat://HospitalDetail?user_id=${id}`)
    } else {
      window.location.href = `https://applink.rongcat.com/hospital/${id}`
    }
  }
}

export function goLabel(id, text) {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    const app = new nativeApp()
    if (app.isInApp()) {
      window.rcatAppInject.launchNative(`rcat://LabelDetail?topic_id=${id}&title=${text}`)
    } else {
      window.location.href = `https://applink.rongcat.com/`
    }
  }
}

export function goLogin() {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    const app = new nativeApp()
    if (app.isInApp()) {
      window.rcatAppInject.launchNative('rcat://Login')
    }
  }
}

export function targetAppOrWx(id) {
  if (isBrowser()) {
    // eslint-disable-next-line new-cap
    var app = new nativeApp()
    if (app.isInApp()) {
      window.rcatAppInject.launchNative('rcat://GoodsDetail?goods_id=' + id)
    } else if (isWx()) {
      isWxMini().then(res => {
        if (res) {
          // eslint-disable-next-line no-undef
          wx.miniProgram.navigateTo({
            url: '/pages/goods_detail/new_goods_detail?goods_id=' + id
          })
          return false
        } else {
          window.location.href = `https://applink.rongcat.com/product/${id}`
          return false
        }
      })
    } else {
      if (deviceType(window.navigator.userAgent).type === 'pc') {
        window.location.href = `https://rongcat.com/product/${id}`
      } else {
        window.location.href = `https://applink.rongcat.com/product/${id}`
      }
    }
  }
}

export function isWx() {
  const UA = window.navigator.userAgent
  return !!(UA.match(/micromessenger/ig) || UA.match(/MicroMessenger/ig))
}

export async function isWxMini() {
  var isWxMini = false
  if (isWx()) {
    // eslint-disable-next-line no-inner-declarations
    function getIsWxMini() {
      return new Promise((resolve) => {
        // eslint-disable-next-line no-undef
        wx.miniProgram.getEnv(function(res) {
          resolve(res.miniprogram)
        })
      })
    }
    // eslint-disable-next-line no-inner-declarations
    async function awaitGetIsWxMini() {
      return await getIsWxMini()
    }
    isWxMini = awaitGetIsWxMini()
    return isWxMini
  }
}
